/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Principal;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;

/**
 * 
 * @author hugo
 */
public class EscenarioGrafico extends Canvas {

    private int ancho;
    private int alto;
    private BufferedImage bi_fondo;
    private boolean flagEscenario;
    private int[][] mapa;

    public EscenarioGrafico(int alto, int ancho) {
        mapa = new int[15][15];
        flagEscenario = false;
        this.ancho = ancho;
        this.alto = alto;
        this.setBounds(0, 0, this.alto, this.ancho);
        //cargando Imagen de fondo
        //bi_fondo=cargarImagen("img/Escenario.jpg");


    }

    public final BufferedImage cargarImagen(String nombre) {
        URL url = null;
        try {
            url = getClass().getClassLoader().getResource(nombre);
            return ImageIO.read(url);
        } catch (IOException ex) {
            System.out.println("No se pudo cargar la imagen " + nombre + " de " + url);
            System.out.println("El error fue : ");
            Logger.getLogger(EscenarioGrafico.class.getName()).log(Level.SEVERE, null, ex);

            return null;
        }
    }

    @Override
    public void paint(Graphics grphcs) {
        super.paint(grphcs);
        //Pintando imagen cargada
        grphcs.drawImage(bi_fondo, 0, 0, alto, ancho, this);
        if (flagEscenario) {
            paintEscenario(this.mapa);
        }
    }

    @Override
    public void repaint() {
        super.repaint();
        Graphics graph = getGraphics();
        graph.drawImage(bi_fondo, 0, 0, alto, ancho, this);
        if (flagEscenario) {
            paintEscenario(this.mapa);
        }
    }
    
//Sirve para pintar el mapa con coores//
    public void paintEscenario(int[][] mapa) {
        Graphics graphcs = this.getGraphics();
        graphcs.drawImage(bi_fondo, 0, 0, alto, ancho, this);
        int cordx = 0;
        int cordy = 0;
        int factorx = alto / 15;
        int factory = ancho / 15;
        for (int idx = 0; idx < 15; idx++) {
            cordx = idx * factorx;

            for (int idy = 0; idy < 15; idy++) {
                cordy = idy * factory;
                int valMap = mapa[idx][idy];
                graphcs.drawRect(cordx, cordy, 39, 39);
                this.mapa[idx][idy] = valMap;
                switch (valMap) {
                    //obstaculo
                    case 0:
                        graphcs.setColor(Color.BLACK);
                        graphcs.fillRect(cordx, cordy, 39, 39);
                        graphcs.drawString("S", cordx + 20, cordy + 20);
                        graphcs.setColor(null);
                        break;
                    //Tierra
                    case 1:
                        graphcs.setColor(Color.PINK);
                        graphcs.fillRect(cordx, cordy, 39, 39);
                        break;
                    //Bosque
                    case 2:
                        graphcs.setColor(Color.GREEN);
                        graphcs.fillRect(cordx, cordy, 39, 39);
                        break;
                    //Pantano
                    case 3:
                        graphcs.setColor(Color.MAGENTA);
                        graphcs.fillRect(cordx, cordy, 39, 39);
                        break;
                    //Arena
                    case 4:
                        graphcs.setColor(Color.YELLOW);
                        graphcs.fillRect(cordx, cordy, 39, 39);

                    //Agua
                    case 5:
                        graphcs.setColor(Color.BLUE);
                        graphcs.fillRect(cordx, cordy, 39, 39);
                        break;
                    //Arena
                    case 6:
                        graphcs.setColor(Color.WHITE);
                        graphcs.fillRect(cordx, cordy, 39, 39);
                        break;

                }
                                
                graphcs.setColor(Color.DARK_GRAY);

            }
        }
        flagEscenario = true;
    }
}
